package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.SysDictDataMapper;
import com.itheima.pojo.*;
import com.itheima.utils.IdWorker;
import com.itheima.dao.AdminMapper;
import com.itheima.dao.SysDictTypeMapper;
import com.itheima.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class SysDictTypeImpl implements SysDictTypeService {

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public PageInfo<SysDictType> getList(SysDictTypeVO sysDictTypeVO) {

        PageHelper.startPage(sysDictTypeVO.getCurrentPage(),sysDictTypeVO.getPageSize());

        Example example = new Example(SysDictType.class);
        Example.Criteria criteria = example.createCriteria();
        if(sysDictTypeVO.getDictType()!=null&&!"".equals(sysDictTypeVO.getDictType())){
            criteria.andEqualTo("dictType",sysDictTypeVO.getDictType());
        }
        if(sysDictTypeVO.getDictName()!=null&&!"".equals(sysDictTypeVO.getDictName())){
            criteria.andEqualTo("dictName",sysDictTypeVO.getDictName());
        }
        List<SysDictType> sysDictTypes = sysDictTypeMapper.selectByExample(example);
        return new PageInfo<>(sysDictTypes);
    }

    @Override
    public void add(SysDictTypeVO sysDictTypeVO) {

        IdWorker idWorker = new IdWorker();
        long uid = idWorker.nextId();
        String suid = String.valueOf(uid);
        SysDictType sysDictType1 = JSON.parseObject(JSON.toJSONString(sysDictTypeVO), SysDictType.class);
        sysDictType1.setUid(suid);
        Admin admin = adminMapper.findAdminByUsername("admin");
        String adminId = admin.getUid();
        sysDictType1.setCreateByUid(adminId);
        sysDictType1.setUpdateByUid(adminId);
        sysDictType1.setCreateTime(new Date());
        sysDictType1.setUpdateTime(new Date());
        sysDictTypeMapper.insertSelective(sysDictType1);
    }

    @Override
    @Transactional
    public Result deleteBatch(List<SysDictTypeVO> sysDictTypeVOList) {
        //如果该字典类型下有字典数据，则不能删除该字典类型
        for (SysDictTypeVO sysDictTypeListVO : sysDictTypeVOList) {

            String uid = sysDictTypeListVO.getUid();

            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictTypeUid(uid);
            SysDictData sysDictData1 = sysDictDataMapper.selectOne(sysDictData);
            if (sysDictData1!=null){
                return new Result(StatusCode.OK,"该字典分类下有字典数据");
            }
            sysDictTypeMapper.deleteByPrimaryKey(uid);
        }
        return new Result(StatusCode.OK,"删除成功");
    }

    @Override
    public void edit(SysDictTypeVO sysDictTypeVO) {
        sysDictTypeVO.setUpdateTime(new Date());
        SysDictType sysDictType = JSON.parseObject(JSON.toJSONString(sysDictTypeVO), SysDictType.class);
        Admin admin = adminMapper.findAdminByUsername("admin");
        String adminId = admin.getUid();
        sysDictType.setCreateByUid(adminId);
        sysDictType.setUpdateByUid(adminId);
        sysDictTypeMapper.updateByPrimaryKey(sysDictType);
    }
}
